検索用にPDF内のテキストをフォーマット維持しつつNotionへ貼り付けてみた

検索用にPDF内のテキストをフォーマット維持しつつNotionへ貼り付けてみた

Notion内でPDFテキストを検索してもらうための一手段として、PDF内のテキストをダンプしてNotionに貼り付けてみました。
Clock Icon2024.09.04

一部問い合わせの回答用向けとして、大体の疑問を解消できるPDFがあればNotion内に埋め込んだりしています。が、Notionの検索はPDF内のテキストまではカバーしてくれません。気が付かなければ無いものと同然です。NotionのインポートにPDFも選択肢として用意されていますが、期待通りの結果になるのかはPDF次第です。

PDF内のテキスト量がそれほどでもない場合は質問する側もPDFを直接見てくれるでしょうが、PDF内にぎっしりとテキストが詰まっている場合は期待できません。

安定してNotionの検索機能にてテキストをマッチさせることを目的にとして、PDF内のレイアウトを維持させつつNotion側にテキストを貼り付けてみました。

PDFtoTextのインストール

PDFからテキストを取得する手段として利用します。

brew install poppler

テキストのダンプ

ポイントはフォーマットの維持となります。単純にダンプした場合はリストや改行駆除等で元文を汲み取れない程に構成維持されない可能性があるためです。

% pdftotext -h                                                                                                                             [~]
pdftotext version 24.04.0
Copyright 2005-2024 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011, 2022 Glyph & Cog, LLC
Usage: pdftotext [options] <PDF-file> [<text-file>]
  -f <int>             : first page to convert
  -l <int>             : last page to convert
  -r <fp>              : resolution, in DPI (default is 72)
  -x <int>             : x-coordinate of the crop area top left corner
  -y <int>             : y-coordinate of the crop area top left corner
  -W <int>             : width of crop area in pixels (default is 0)
  -H <int>             : height of crop area in pixels (default is 0)
  -layout              : maintain original physical layout
  -fixed <fp>          : assume fixed-pitch (or tabular) text
  -raw                 : keep strings in content stream order
  -nodiag              : discard diagonal text
  -htmlmeta            : generate a simple HTML file, including the meta information
  -tsv                 : generate a simple TSV file, including the meta information for bounding boxes
  -enc <string>        : output text encoding name
  -listenc             : list available encodings
  -eol <string>        : output end-of-line convention (unix, dos, or mac)
  -nopgbrk             : don't insert page breaks between pages
  -bbox                : output bounding box for each word and page size to html. Sets -htmlmeta
  -bbox-layout         : like -bbox but with extra layout bounding box data.  Sets -htmlmeta
  -cropbox             : use the crop box rather than media box
  -colspacing <fp>     : how much spacing we allow after a word before considering adjacent text to be a new column, as a fraction of the font size (default is 0.7, old releases had a 0.3 default)
  -opw <string>        : owner password (for encrypted files)
  -upw <string>        : user password (for encrypted files)
  -q                   : don't print any messages or errors
  -v                   : print copyright and version info
  -h                   : print usage information
  -help                : print usage information
  --help               : print usage information
  -?                   : print usage information

基本となるオプションは-layoutです。

以下はメンバーズポータルの操作ガイドをダンプする際の例です。

 pdftotext -layout -q classmethod-Members-Portal-Operation-Guide.pdf classmethod-Members-Portal-Operation-Guide.txt

ヘッダとフッタを除去したい場合

ダンプしたテキストからページ改行コードを目安に手作業で除去する方法もありますが、X始点(-x)、Y始点(-y) 、右端(-W)、下端(-H)を駆使する手もあります。

これはPDFの各ページから指定された幅と高さ内に含まれるテキストをダンプ対象とするオプションで、指定領域内に含まれなかったテキストはダンプされません。これはヘッダとフッダも対象に含まれます。

pdftotext -layout -q -x 0 -y 60 -H 700 -W 1200 classmethod-Members-Portal-Operation-Guide.pdf classmethod-Members-Portal-Operation-Guide.txt

Notionに貼り付ける

個人的にはフォーマットの都合上、コードブロックか引用ブロックとしています。暫くするとNotionの検索にマッチすることを確認済みです。

あとがき

PDF内のテキストを対象とした検索は難儀する領域ですが、ダンプしたテキストを活用することでカバーできることもあります。常に期待した結果が得られるとは限らないため、まずは試しにダンプしてみることをおすすめします。

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.